package demo3
{
	import copyengine.utils.KeyCode;

	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;

	import nape.geom.Vec2;

	public final class MouseCutManger
	{
		private var mouseCutLayer:Sprite;

		private var isDrawingLine:Boolean=false;

		private var cutLineStartPoint:Vec2;
		private var cutLineEndPoint:Vec2;

		private var onCutCallBack:Function;

		private var currentPushKeycode:uint;

		public function MouseCutManger()
		{
		}

		public function initialize(_container:DisplayObjectContainer, _onCutCallBack:Function):void
		{
			mouseCutLayer=new Sprite();
			onCutCallBack=_onCutCallBack;

			_container.addChild(mouseCutLayer);
			mouseCutLayer.stage.addEventListener(MouseEvent.MOUSE_DOWN, onReceiveMouseDownEvent);
			mouseCutLayer.stage.addEventListener(MouseEvent.MOUSE_UP, onReceiveMouseUpEvent);
			mouseCutLayer.stage.addEventListener(MouseEvent.MOUSE_MOVE, onReceiveMouseMoveEvent);
			mouseCutLayer.stage.addEventListener(KeyboardEvent.KEY_DOWN, onReceiveOnKeyDown);
			mouseCutLayer.stage.addEventListener(KeyboardEvent.KEY_UP, onReceiveOnKeyUp);

			cutLineStartPoint=new Vec2();
			cutLineEndPoint=new Vec2();
		}

		protected function onReceiveOnKeyUp(event:KeyboardEvent):void
		{
			currentPushKeycode=0;
		}

		protected function onReceiveOnKeyDown(event:KeyboardEvent):void
		{
			currentPushKeycode=event.keyCode;
		}

		protected function onReceiveMouseMoveEvent(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if (isDrawingLine)
			{
				mouseCutLayer.graphics.clear();
				mouseCutLayer.graphics.moveTo(cutLineStartPoint.x, cutLineStartPoint.y);
				mouseCutLayer.graphics.lineStyle(3, 0xff0000);
				mouseCutLayer.graphics.lineTo(event.stageX, event.stageY);
			}
		}

		protected function onReceiveMouseUpEvent(event:MouseEvent):void
		{
			if (isDrawingLine)
			{
				cutLineEndPoint.x=event.stageX;
				cutLineEndPoint.y=event.stageY;
				onCutCallBack(cutLineStartPoint, cutLineEndPoint)
			}

			mouseCutLayer.graphics.clear();
			isDrawingLine=false;
		}

		protected function onReceiveMouseDownEvent(event:MouseEvent):void
		{
			if (currentPushKeycode == KeyCode.SHIFT)
			{
				cutLineStartPoint.x=event.stageX;
				cutLineStartPoint.y=event.stageY;
				isDrawingLine=true;
			}
		}



	}
}
